package com.water.smartfarm.main;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.water.smartfarm.R;
import com.water.smartfarm.tools.Const;

public class SetFragment extends Fragment implements View.OnClickListener {
    private View mRootView = null;

    private EditText upTempInput;
    private EditText downTempInput;
    private EditText upHumInput;
    private EditText downHumInput;
    private EditText upLightInput;
    private EditText downLightInput;
    private EditText upCo2Input;
    private EditText downCo2Input;

    private Button resetBtn;
    private Button saveBtn;

    String upTemp;
    String downTemp;
    String upHum;
    String downHum;
    String upLight;
    String downLight;
    String upCo2;
    String downCo2;
    SharedPreferences mySharedPreferences;
    SharedPreferences.Editor editor ;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mRootView = inflater.inflate(R.layout.fragment_set, container, false);
        initView();
        return mRootView;
    }

    private void initView() {

        saveBtn = (Button) mRootView.findViewById(R.id.save_btn);
        saveBtn.setOnClickListener(this);
        resetBtn = (Button) mRootView.findViewById(R.id.reset_btn);
        resetBtn.setOnClickListener(this);

        upTempInput = (EditText) mRootView.findViewById(R.id.up_temp_input);
        downTempInput = (EditText) mRootView.findViewById(R.id.down_temp_input);
        upHumInput = (EditText) mRootView.findViewById(R.id.up_hum_input);
        downHumInput = (EditText) mRootView.findViewById(R.id.down_hum_input);
        upLightInput = (EditText) mRootView.findViewById(R.id.up_light_input);
        downLightInput = (EditText) mRootView.findViewById(R.id.down_light_input);
        upCo2Input = (EditText) mRootView.findViewById(R.id.up_co2_input);
        downCo2Input = (EditText) mRootView.findViewById(R.id.down_co2_input);

        mySharedPreferences = getActivity().getSharedPreferences("data",
                Activity.MODE_PRIVATE);
        editor = mySharedPreferences.edit();
    }


    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.reset_btn:
                reset();
                break;
            case R.id.save_btn:
                save();
                break;
        }
    }

    private void save() {
        upTemp = upTempInput.getText().toString();
        downTemp = downTempInput.getText().toString();
        upHum = upHumInput.getText().toString();
        downHum = downHumInput.getText().toString();
        upLight = upLightInput.getText().toString();
        downLight = downLightInput.getText().toString();
        upCo2 = upCo2Input.getText().toString();
        downCo2 = downCo2Input.getText().toString();


        editor.putString(Const.up_temp, upTemp);
        editor.putString(Const.down_temp, downTemp);
        editor.putString(Const.up_hum, upHum);
        editor.putString(Const.down_hum, downHum);
        editor.putString(Const.up_light, upLight);
        editor.putString(Const.down_light, downLight);
        editor.putString(Const.up_co2, upCo2);
        editor.putString(Const.down_co2, downCo2);

        editor.commit();
    }

    private void reset() {
        upTemp = "";
        downTemp = "";
        upHum = "";
        downHum = "";
        upLight = "";
        downLight = "";
        upCo2 = "";
        downCo2 = "";
        upTempInput.setText(upTemp);
        downTempInput.setText(downTemp);
        upHumInput.setText(upHum);
        downHumInput.setText(downHum);
        upLightInput.setText(upLight);
        downLightInput.setText(downLight);
        upCo2Input.setText(upCo2);
        downCo2Input.setText(downCo2);
        editor.putString(Const.up_temp, upTemp);
        editor.putString(Const.down_temp, downTemp);
        editor.putString(Const.up_hum, upHum);
        editor.putString(Const.down_hum, downHum);
        editor.putString(Const.up_light, upLight);
        editor.putString(Const.down_light, downLight);
        editor.putString(Const.up_co2, upCo2);
        editor.putString(Const.down_co2, downCo2);
        editor.commit();
    }
}
